package edu.fiu.cis.personalize.server.graph.update;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import edu.fiu.cis.personalize.server.clean.Stemmer;
import edu.fiu.cis.personalize.server.util.SetUtils;

public abstract class SimilarityMeasure {

	public enum SimilarityType {
		JACCARD
	}

	private static Map<SimilarityType, SimilarityMeasure> _m = new HashMap<SimilarityMeasure.SimilarityType, SimilarityMeasure>();

	static {
		_m.put(SimilarityType.JACCARD, new JaccardSimilarity());
	}

	public static double computeSimilarity(String s1, String s2,
			SimilarityType type) {
		return _m.get(type).similarity(s1, s2);

	}

	public abstract double similarity(String str1, String str2);

	public static double computeSimilarity(String s1, String s2) {
		return computeSimilarity(s1, s2, SimilarityType.JACCARD);
	}

}

class JaccardSimilarity extends SimilarityMeasure {

	@Override
	public double similarity(String str1, String str2) {
		Set<String> s1 = Stemmer.stem(str1);
		Set<String> s2 = Stemmer.stem(str2);

		double n = SetUtils.intersect(s1, s2).size();
		double d = SetUtils.union(s1, s2).size();

		return d != 0 ? n / d : 0;
	}

}